package com.example.calendarprovider.adapter;


import android.annotation.SuppressLint;
import android.graphics.Point;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.calendarprovider.R;
import com.example.calendarprovider.bean.ImageItem;
import com.example.calendarprovider.utils.SizeUtils;

import java.util.ArrayList;
import java.util.List;

public class ResultImageAdapter extends  RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private static final String TAG = "ResultImageAdapter";
    private List<ImageItem> mImageItems=new ArrayList<>();

    private int mHolizonalCount=1;
    private static final int IMAGEVIEW=1;
    private static  final  int JIAVIEW=2;
    private int selectMax=9;



    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType==IMAGEVIEW){
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_item, parent, false);

//        Point point = SizeUtils.getScreenSize(parent.getContext());
//        RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(point.x / mHolizonalCount, point.x / mHolizonalCount);
            //   itemView.setLayoutParams(layoutParams);

            return new ImageViewInnerHolder(itemView);
        }else {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.jia_item, parent, false);
            return new JiaViewInnerHolder(itemView);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @SuppressLint("ResourceType")
    @Override
    public void onBindViewHolder(@NonNull  RecyclerView.ViewHolder holder, int position) {

        if(holder instanceof ImageViewInnerHolder){
            View itemView = holder.itemView;
            Point point = SizeUtils.getScreenSize(itemView.getContext());
            RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(point.x/mHolizonalCount , point.x / mHolizonalCount);
            itemView.setLayoutParams(layoutParams);
            ImageView imageView = holder.itemView.findViewById(R.id.image_iv);
            ImageView imageCancel = holder.itemView.findViewById(R.id.cancel2);
            ImageItem imageItem = mImageItems.get(position);
            Glide.with(imageView.getContext()).load(imageItem.getPath()).into(imageView);
            imageCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnDeleteImageListener.onClickDeleteEvent(position);
                }
            });

        }else if(holder instanceof JiaViewInnerHolder){
            View itemView = holder.itemView;
            Point point = SizeUtils.getScreenSize(itemView.getContext());
            RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(point.x / 3, point.x / 3);
            itemView.setLayoutParams(layoutParams);
            ImageView imageView = holder.itemView.findViewById(R.id.image_jia);
            Glide.with(imageView.getContext()).load(R.mipmap.jia).into(imageView);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnJiaClickListener.onClickJiaEvent();
                }
            });
        }
    }

    //返回的个数,以满足不满9张可以显示添加的那个加号
    @Override
    public int getItemCount() {
        if(mImageItems.size()<selectMax){
            return mImageItems.size()+1;//确保加号在最后面显示
        }
       else{
           return mImageItems.size();
        }
    }

    //返回的类型
    public  int getItemViewType(int position){
        if(isShowAddItem(position)){
            return JIAVIEW;//添加加号
        }else {
            return IMAGEVIEW;//添加图片
        }
    }




    /**

     * @param position

     * @return 是否显示添加图片的按钮

     */

    private boolean isShowAddItem(int position) {

        int size = mImageItems.size() == 0 ? 0 : mImageItems.size();// 第0个位置,集合为0 返回为true; 第一个位置position=1,list.size=3,size=3 返回false
        Log.d(TAG, "mImageItems.size() "+mImageItems.size()+"position"+position);
        //因为position是从0开始的，加号刚开始是的position是0,而mImageSize也是0，所以最先加载的是加号，而后面当你点击加号跳转到
        //媒体库获取图片时,适配器会再次更新选择的数据，此时的getViewType这个方法每一列数据都会执行到，比如你再媒体库选择了三张图片，
        // 三张图片的position,依次是0,1,2。而mImageItem.size为3。又加号的position由于前面多增加的图片位置已经移动到3，返回true,所以最后会显示出加号
        return position == size;

    }
    private onJiaClickListener mOnJiaClickListener=null;

    public void setOnJiaClickListener(onJiaClickListener onJiaClickListener) {
        this.mOnJiaClickListener = onJiaClickListener;
    }
    public void setOnDeleteImageListener(onDeleteImageListener onDeleteImageListener) {
        this.mOnDeleteImageListener = onDeleteImageListener;
    }
    private onDeleteImageListener mOnDeleteImageListener=null;
    public interface onJiaClickListener{
        void onClickJiaEvent();
   }
   public interface onDeleteImageListener{
        void onClickDeleteEvent(int position);
   }

    public void setData(List<ImageItem> result, int horizontalCount,boolean flag) {
        this.mHolizonalCount=horizontalCount;
        if(flag==true){
            //当是点击删除的时候要清除掉加的时候的数据，
            mImageItems.clear();
            mImageItems.addAll(result);
//            mImageItems.addAll(mAfterImageItem);
            Log.d(TAG, "setData: 点击删除后的size"+mImageItems.size());
//            Log.d(TAG, "setData: 点击删除后的AfterImageItem"+mAfterImageItem.size());
        }else {
            mImageItems.addAll(result);
           //当点击加号的时候
            Log.d(TAG, "setData: 点击增加后的size"+mImageItems.size());
        }

        notifyDataSetChanged();
    }

    public class ImageViewInnerHolder extends RecyclerView.ViewHolder {
        public ImageViewInnerHolder(@NonNull  View itemView) {
            super(itemView);
            itemView.findViewById(R.id.image_check).setVisibility(View.GONE);
        }
    }

    private class JiaViewInnerHolder extends RecyclerView.ViewHolder {
        public JiaViewInnerHolder(View itemView) {
            super(itemView);
            itemView.findViewById(R.id.image_jia);
        }
    }
}
